/*
 * @Author: viivLgr 
 * @Date: 2018-03-26 10:52:48 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-26 11:23:37
 */
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _util = require('util/util.js');
var _payment = require('service/payment-service.js');
var templateIndex = require('./index.string');

var page = {
    data: {
        orderNum: _util.getUrlParam('orderNumber')
    },
    init: function () {
        this.onLoad();
    },
    onLoad: function () {
        // 加载支付信息
        this.loadPayment();
    },
    loadPayment() {
        var _this = this,
            html = '',
            $pageWrap = $('.page-wrap');
        $pageWrap.html('<div class="loading"></div>');
        _payment.getPaymentInfo(this.data.orderNum, function (res) {
            html = _util.renderHtml(templateIndex, res);
            $pageWrap.html(html);
            _this.listenOrderStatus();
        }, function (err) {
            $pageWrap.html('<p class="error-tip">' + err + '</p>');
        });
    },
    // 监听订单状态 轮询
    listenOrderStatus: function () {
        var _this = this;
        this.paymentTimer = window.setInterval(function () {
            _payment.getPaymentStatus(_this.data.orderNum, function (res) {
                if (res == true) {
                    window.location.href = './result.html?type=payment&orderNumber=' + _this.data.orderNum;
                }
            }, function (err) {
                $pageWrap.html('<p class="error-tip">' + err + '</p>');
            });
        }, 5e3);
    }
};

$(function () {
    page.init();
});
